//============================================================================================
/**
 * @file	poke_select_call.c
 * @brief	ポケモン選択呼び出し
 * @author	Nozomu Saito
 *
 * モジュール名：PokeSelectCall_
 */
//============================================================================================
#include "common.h"
#include "fieldsys.h"
#include "field/field.h"
#include "savedata/zukanwork.h"
#include "savedata/get_poke.h"
#include "system/wipe.h"
#include "system/brightness.h"

#include "demo/first_poke_select.h"

#include "itemtool/itemsym.h"
#include "zonedata.h"

#include "poke_select_call.h"

#define	GROUND_ID_ALL		(12)	//それ以外（一応定義）戦闘defから引用

static BOOL PokeSelectEvent(GMEVENT_CONTROL * event);

typedef struct{
	int	Seq;
	POKESELECT_PEARENT_DATA *Parent;
}PSEL_CALL_EVENT_WORK;

typedef enum
{
	PSEL_CALL_FADEOUT,
	PSEL_CALL_FADEOUT_WAIT,
	PSEL_CALL_PROC,
	PSEL_ADD_POKE,
	PSEL_CALL_FADEIN,
	PSEL_CALL_FADEIN_WAIT,
}PSEL_CALL_SEQ;

//==============================================================================
/**
 * イベント作成
 *
 * @param	fsys		フィールドシステムポインタ
 *
 * @retval  none
 */
//==============================================================================
void PokeSelectCall_SetupEvent(FIELDSYS_WORK * fsys)
{
	// イベントワーク作成
	PSEL_CALL_EVENT_WORK *wk = sys_AllocMemoryLo(HEAPID_WORLD, sizeof(PSEL_CALL_EVENT_WORK));
	wk->Seq = 0;
	FieldEvent_Call(fsys->event, PokeSelectEvent, wk);
}

//==============================================================================
/**
 * イベント
 *
 * @param	event	イベントポインタ
 *
 * @retval  BOOL	TRUE:イベント終了　FALSE：イベント継続
 */
//==============================================================================
static BOOL PokeSelectEvent(GMEVENT_CONTROL * event)
{
	FIELDSYS_WORK * fsys = FieldEvent_GetFieldSysWork(event);
	PSEL_CALL_EVENT_WORK *wk  = FieldEvent_GetSpecialWork(event);

	switch(wk->Seq){
	case PSEL_CALL_FADEOUT:
		WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEOUT,
				WIPE_TYPE_FADEOUT,0x0000,COMM_BRIGHTNESS_SYNC,1,HEAPID_FIELD
				);
		wk->Seq = PSEL_CALL_FADEOUT_WAIT;
		break;
	case PSEL_CALL_FADEOUT_WAIT:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		{
			int i;
			int heap;
			int first_mons_no[3] = {152,155,158};
			int level = 5;
			u16 itemno = 0;
			int place_id = ZoneData_GetPlaceNameID(fsys->location->zone_id);
			int ground_id = GROUND_ID_ALL;

			heap = HEAPID_WORLD;
			wk->Parent = sys_AllocMemoryLo(heap, sizeof(POKESELECT_PEARENT_DATA));
			wk->Parent->SelectBallNo = 0;
			wk->Parent->Config = SaveData_GetConfig( fsys->savedata );
			//手持ちに加える3体のポケデータの作成
			for (i=0;i<3;i++){
				BOOL result;
				POKEMON_PARAM *poke_param;
				u32 buf;
				MYSTATUS * my;
				poke_param = &(wk->Parent->PokeParam[i]);
				my = SaveData_GetMyStatus(fsys->savedata);
//				poke_param = PokemonParam_AllocWork(heap);
				PokeParaInit(poke_param);
				PokeParaSet(poke_param, first_mons_no[i], level, POW_RND, RND_NO_SET, 0, ID_NO_SET, 0);
				PokeParaGetInfoSet(poke_param, my, ITEM_MONSUTAABOORU, place_id, ground_id, heap);
				buf = itemno;
				PokeParaPut(poke_param, ID_PARA_item, &buf);
			}

			FirstPokeSelect_SetProc(fsys, wk->Parent);
			FieldSystemProc_SeqHoldEnd();
		}
		wk->Seq = PSEL_CALL_PROC;
		break;
	case PSEL_CALL_PROC:
		//プロセス終了待ち
		if(FieldEvent_Cmd_WaitSubProcEnd(fsys)){
			break;
		}
		wk->Seq = PSEL_ADD_POKE;
		break;
	case PSEL_ADD_POKE:
		//ポケパーティに追加
		{
			BOOL result;
			POKEPARTY * party;
			POKEMON_PARAM *poke_param;
			ZUKAN_WORK* zw = SaveData_GetZukanWork( fsys->savedata );
			party = SaveData_GetTemotiPokemon(fsys->savedata);

			poke_param = &(wk->Parent->PokeParam[ wk->Parent->SelectBallNo ]);
			result = PokeParty_Add(party, poke_param);
			if (result) {
				SaveData_GetPokeRegister( fsys->savedata, poke_param );
			}
			// 図鑑に登録
			ZukanWork_SetPokeGet( zw, PokeParty_GetMemberPointer( party, 0) );
//			ZukanWork_SetZenkokuZukanFlag( zw );
		}
		wk->Seq = PSEL_CALL_FADEIN;
		//フィールドマッププロセス復帰
		FieldEvent_Cmd_SetMapProc(fsys);
		break;
	case PSEL_CALL_FADEIN:
		//フィールドプロセス開始終了待ち
		if(FieldEvent_Cmd_WaitMapProcStart(fsys)){
			//ブラックインリクエスト
			WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEIN,
				WIPE_TYPE_FADEIN,0x0000,COMM_BRIGHTNESS_SYNC,1,HEAPID_FIELD
				);
			wk->Seq = PSEL_CALL_FADEIN_WAIT;
			break;
		}
		break;
	case PSEL_CALL_FADEIN_WAIT:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		// イベントワーク解放
		sys_FreeMemoryEz(wk->Parent);
		sys_FreeMemoryEz(wk);
		return TRUE;
	}

	return FALSE;
}